import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/*
 * @date 2018-07-11
 */
import { getResponse, createPagination } from 'utils/utils';
import { queryStoreRoom, saveStoreRoom as _saveStoreRoom } from "../services/storeRoomService";
export default {
  namespace: 'storeRoom',
  state: {
    storeRoomList: [],
    pagination: {}
  },
  effects: {
    queryStoreRoomList: /*#__PURE__*/_regeneratorRuntime.mark(function queryStoreRoomList(_ref, _ref2) {
      var payload, call, put, storeRoomListData;
      return _regeneratorRuntime.wrap(function queryStoreRoomList$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.t0 = getResponse;
              _context.next = 5;
              return call(queryStoreRoom, payload);

            case 5:
              _context.t1 = _context.sent;
              storeRoomListData = (0, _context.t0)(_context.t1);

              if (!storeRoomListData) {
                _context.next = 10;
                break;
              }

              _context.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  storeRoomList: storeRoomListData.content,
                  pagination: createPagination(storeRoomListData)
                }
              });

            case 10:
            case "end":
              return _context.stop();
          }
        }
      }, queryStoreRoomList);
    }),
    saveStoreRoom: /*#__PURE__*/_regeneratorRuntime.mark(function saveStoreRoom(_ref3, _ref4) {
      var payload, call, storeRoom;
      return _regeneratorRuntime.wrap(function saveStoreRoom$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call;
              _context2.next = 4;
              return call(_saveStoreRoom, payload.inventoryList);

            case 4:
              storeRoom = _context2.sent;
              return _context2.abrupt("return", getResponse(storeRoom));

            case 6:
            case "end":
              return _context2.stop();
          }
        }
      }, saveStoreRoom);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref5) {
      var payload = _ref5.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};